﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="XMLPage.aspx.cs" Inherits="BugSysSolution.XMLAndXSLT.XMLPage" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="../Javascript/jquery-1.8.0.min.js" type="text/javascript"></script>
    <script src="../Javascript/common.js" type="text/javascript"></script>
    <link href="../Style/site.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div id="showxml" style="position: absolute; min-height: 300px; min-width: 300px;">
    </div>
    <div style="position: absolute; top: 315px; left: 200px">
        <a id="prev" href="#">上一页</a> <a id="next" href="#">下一页</a>
    </div>
    </form>
</body>
</html>
<script type="text/javascript">
    var loaddiv = ShowLoadingDiv($("#showxml"));
    var arr = new Array();
    var tagNameArr = new Array();

    function getDataSource() {
        SendAjax("get", "test.xml", null, "xml", function (data) {
            var xmlobj = $(data).find("cd");

            //获取xml节点的tagName
            xmlobj.eq(0).children().each(function () {
                tagNameArr.push($(this).get(0).tagName);
            })
            //组装json
            xmlobj.each(function () {
                var tempjson = "";
                tempjson += "{";
                for (var i = 0; i < tagNameArr.length; i++) {
                    tempjson += tagNameArr[i] + ":'" + $(this).find(tagNameArr[i]).eq(0).text() + "',";
                }
                tempjson = tempjson.substring(0, tempjson.lastIndexOf(','));
                tempjson += "}";
                var jsonobj = eval('(' + tempjson + ')');

                arr.push(jsonobj);
            })
            $(loaddiv).data("source", arr);
            $(loaddiv).data("proto", tagNameArr);
            $(loaddiv).css("display", "none");
        });
    }

    function pageload() {
        getDataSource();
    }

    function showhtml(data) {
        var data1 = $(loaddiv).data("source");
        var data2 = $(loaddiv).data("proto");
        if (data1 == null || data2 == null) {
            alert("正在请求数据中.....");
            return;
        }

//        for (var i = 0; i < data1.length; i++) {
//            if (data1[i][tagNameArr[0]] == "Still got the blues") {
//                alert("第" + (parseInt(i) + 1) + "个");
//            }
//        }

        var html = "";
        html += "<table class='tab'>";
        html += "<tr>";
        for (var i = 0; i < data2.length; i++) {
            html += "<td>" + data2[i] + "</td>";
        }
        html += "</tr>";

        for (var i = 0; i < data.length; i++) {
            html += "<tr>";
            for (var j = 0; j < data2.length; j++) {
                html += "<td>" + data[i][data2[j]] + "</td>";
            }
            html += "</tr>";
        }

        html += "</table>";
        $("#showxml").html(html);
    }

    $("#prev").click(function () {
        var temparr = $(loaddiv).data("source");
        temparr.length = 5;
        showhtml(temparr);
    })

    pageload();
</script>
